/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util.analyze;

import java.text.NumberFormat;
import java.util.Iterator;

import soccerscope.model.ClientScene;
import soccerscope.model.GameEvent;
import soccerscope.model.Param;
import soccerscope.model.WorldModel;

public class TopXAnalyzer extends PassAnalyzer {

	public static String NAME = "Top3 Recv X";

	private float receiverLeftX;

	private float receiverLeftMinX;

	private float receiverLeftMaxX;

	private float receiverRightX;

	private float receiverRightMinX;

	private float receiverRightMaxX;

	@Override
	public void init() {
		super.init();
	}

	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public Object getValueAt(int col, int fromTime, int toTime) {
		count(fromTime, toTime);
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumIntegerDigits(2);
		nf.setMaximumFractionDigits(2);
		switch (col) {
		case ROW_NAME:
			return getName();
		case LEFT:
			if (this.lcount == -1)
				return "--";
			// return "(" + nf.format(receiverLeftMinX) + " : " +
			// nf.format(receiverLeftX / lcount) +
			// " : " + nf.format(receiverLeftMaxX) + ")";
			return nf.format(this.receiverLeftX / this.lcount);
		case RIGHT:
			if (this.rcount == -1)
				return "--";
			// return "(" + nf.format(-receiverRightMaxX) + " : " +
			// nf.format(-receiverRightX / rcount) +
			// " : " + nf.format(-receiverRightMinX) + ")";
			return nf.format(-this.receiverRightX / this.rcount);
		default:
			return " ";
		}
	}

	@Override
	public void count(int fromTime, int toTime) {
		Iterator it = PassAnalyzer.getPassLeftList().iterator();
		this.lcount = 0;
		this.receiverLeftX = 0;
		this.receiverLeftMinX = 100;
		this.receiverLeftMaxX = -100;
		while (it.hasNext()) {
			Pass pass = (Pass) it.next();
			if (fromTime <= pass.sender.time && pass.receiver.time <= toTime) {
				ClientScene clientScene = (ClientScene) WorldModel
						.getInstance().getSceneSet().getScene(
								pass.receiver.time);
				float x = clientScene.player[pass.receiver.unum - 1].pos.x;
				int n_front = 0;
				for (int i = 0; i < Param.MAX_PLAYER; i++) {
					if (i != pass.receiver.unum - 1
							&& clientScene.player[i].pos.x > x)
						n_front++;
				}
				if (n_front >= 3) {
					clientScene = (ClientScene) WorldModel.getInstance()
							.getSceneSet().getScene(pass.sender.time);
					x = clientScene.player[pass.receiver.unum - 1].pos.x;
					n_front = 0;
					for (int i = 0; i < Param.MAX_PLAYER; i++) {
						if (i != pass.receiver.unum - 1
								&& clientScene.player[i].pos.x > x)
							n_front++;
					}
				}
				if (n_front < 3) {
					this.receiverLeftX += x;
					this.lcount++;
					if (this.receiverLeftMinX > x)
						this.receiverLeftMinX = x;
					if (this.receiverLeftMaxX < x)
						this.receiverLeftMaxX = x;
				}
			}
		}
		if (this.lcount == 0)
			this.lcount = -1;

		it = PassAnalyzer.getPassRightList().iterator();
		this.rcount = 0;
		this.receiverRightX = 0;
		this.receiverRightMinX = 100;
		this.receiverRightMaxX = -100;
		while (it.hasNext()) {
			Pass pass = (Pass) it.next();
			if (fromTime <= pass.sender.time && pass.receiver.time <= toTime) {
				ClientScene clientScene = (ClientScene) WorldModel
						.getInstance().getSceneSet().getScene(
								pass.receiver.time);
				float x = clientScene.player[pass.receiver.unum
						+ Param.MAX_PLAYER - 1].pos.x;
				int n_front = 0;
				for (int i = Param.MAX_PLAYER; i < Param.MAX_PLAYER * 2; i++) {
					if (i != pass.receiver.unum - 1
							&& clientScene.player[i].pos.x < x)
						n_front++;
				}
				if (n_front >= 3) {
					clientScene = (ClientScene) WorldModel.getInstance()
							.getSceneSet().getScene(pass.sender.time);
					x = clientScene.player[pass.receiver.unum
							+ Param.MAX_PLAYER - 1].pos.x;
					n_front = 0;
					for (int i = Param.MAX_PLAYER; i < Param.MAX_PLAYER * 2; i++) {
						if (i != pass.receiver.unum - 1
								&& clientScene.player[i].pos.x < x)
							n_front++;
					}
				}
				if (n_front < 3) {
					this.receiverRightX += x;
					this.rcount++;
					if (this.receiverRightMinX > x)
						this.receiverRightMinX = x;
					if (this.receiverRightMaxX < x)
						this.receiverRightMaxX = x;
				}
			}
		}
		if (this.rcount == 0)
			this.rcount = -1;
	}

	@Override
	public GameEvent analyze(ClientScene clientScene, ClientScene prev) {
		return null;
	}
}
